const app = getApp()
import { index, apply } from '../../../../api/mine/distribution'
import { phone } from '../../../../api/wechat'
import { login } from '../../../../utils/request'

Page({
  data: {
    header: app.global.headerHeight,
    theme: '',
    back: true,
    title: '分销中心',
    loading: true,
    submitLoading: false,
    login: false,

    /**
     * 申请信息
     */
    dialogShow: false,
    join: {},
    readed: true,
    name: '',
    phone: '',
    // 申请信息 End
    bonus: 0, // 可提
    tellered: 0, // 已提
    delay: 0, // 未到期的
    total: 0, // 总额
    protocolShow: false,
    protocol: '',
    team: [],
    template: []
  },
  onLoad () {},
  onShow () {
    this.setData({ loading: true, theme: app.global.theme })
    
    if (app.login()) {
      index().then(res => {
        const { data } = res
        
        // 是否需要审核
        if (data.setting.distribution_join == 20) {
          if (!(data.join && data.join.audit_status == 20)) {
            this.data.dialogShow = true
          }
        }
        
        this.setData({
          bonus: data.bonus,
          tellered: data.tellered,
          delay: data.delay,
          total: data.total,
          team: data.team,
          protocol: data.setting.content,
          template: data.template,
          join: data.join,
          dialogShow: this.data.dialogShow,
          login: false
        })
      })
        .finally(() => {
          this.setData({ loading: false })
        })
    } else {
      this.setData({ login: true })
    }
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () { /* 页面上拉触底事件的处理函数*/ },

  // ===================method================//

  onReaded () {
    this.setData({ readed: !this.data.readed })
  },
  onReading () {
    this.setData({ protocolShow: true })
  },
  setName (event) {
    this.setData({ name: event.detail.value })
  },
  setPhone (data) {
    const that = this
    wx.showLoading({ title: '手机号获取中...' })
    phone(data).then(res => {
      that.setData({ phone: res.data })
    })
      .catch(error => {
        app.toast('网络繁忙，请重试')
      })
      .finally(() => {
        wx.hideLoading()
      })
  },
  getPhoneNumber (event) {
    if (event.detail.errMsg !== 'getPhoneNumber:ok') {
      app.toast('需要您的授权许可才能获取手机号码')
      return false
    }

    const that = this
    const data = {
      iv: event.detail.iv,
      encryptedData: encodeURIComponent(event.detail.encryptedData)
    }

    wx.checkSession({
      success() {
        that.setPhone(data)
      },
      fail () {
        login().then(res => {
          that.setPhone(data)
        })
          .catch(error => {
            app.toast('网络繁忙，请重试')
          })
      }
    })
  },
  handleSubmit () {
    const that = this
    const data = {
      name: that.data.name.trim(),
      phone: that.data.phone
    }

    if (!that.data.readed) {
      app.toast('请勾选并阅读协议')
      return false
    }
    if (data.name == '') {
      app.toast('请输入您的姓名')
      return false
    }
    if (data.phone == '') {
      app.toast('请授权获取您的手机号')
      return false
    }

    wx.requestSubscribeMessage({
      tmplIds: that.data.template,
      complete () {
        that.setData({ submitLoading: true })
        apply(data).then(res => {
          app.toast('待后台审核', function () {
            wx.navigateBack()
          }, 'success')
        })
          .finally(() => {
            that.setData({ submitLoading: false })
          })
      }
    })
  },
  protocolClose () {
    this.setData({ protocolShow: false })
  },
  redirect (event) {
    app.redirect(event.currentTarget.dataset.url)
  }
})